<!DOCTYPE html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon32.ico?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon16.ico?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="template,">










<meta name="description" content="为了看懂各种STL源码必须具备的基本知识。频繁的利用特化和SFINAE，看这些源码就感觉就像在看GHC的实现(bottom-up, 模式匹配?)。">
<meta name="keywords" content="template">
<meta property="og:type" content="article">
<meta property="og:title" content="[Template] type_traits">
<meta property="og:url" content="http://htelepathh.gitee.io/2021/06/02/Template-type-traits/index.html">
<meta property="og:site_name" content="OUTPUT">
<meta property="og:description" content="为了看懂各种STL源码必须具备的基本知识。频繁的利用特化和SFINAE，看这些源码就感觉就像在看GHC的实现(bottom-up, 模式匹配?)。">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2021-06-02T03:22:00.886Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="[Template] type_traits">
<meta name="twitter:description" content="为了看懂各种STL源码必须具备的基本知识。频繁的利用特化和SFINAE，看这些源码就感觉就像在看GHC的实现(bottom-up, 模式匹配?)。">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":true},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://htelepathh.gitee.io/2021/06/02/Template-type-traits/">





  <title>[Template] type_traits | OUTPUT</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">OUTPUT</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">HTelepathH's blog</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-message">
          <a href="/message" rel="section">
            
            留言
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about" rel="section">
            
            关于
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://htelepathh.gitee.io/2021/06/02/Template-type-traits/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="HTelepathH">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/images/MOB.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="OUTPUT">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">[Template] type_traits</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2021-06-02T10:39:12+08:00">
                2021-06-02
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/C/" itemprop="url" rel="index">
                    <span itemprop="name">C++</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>为了看懂各种STL源码必须具备的基本知识。频繁的利用特化和SFINAE，看这些源码就感觉就像在看GHC的实现(bottom-up, 模式匹配?)。<br><a id="more"></a></p>
<h3 id="intergral-constant-C-11"><a href="#intergral-constant-C-11" class="headerlink" title="intergral_constant C++11"></a>intergral_constant C++11</h3><p><code>type_traits</code>的基础。包装某个类型的一个<code>static constant</code>。被仿函数调用和类型转换时返回这个值。<br><figure class="highlight cpp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">template</span>&lt;<span class="class"><span class="keyword">class</span> <span class="title">T</span>, <span class="title">T</span> <span class="title">v</span>&gt;</span></span><br><span class="line"><span class="class"><span class="title">struct</span> <span class="title">integral_constant</span> &#123;</span></span><br><span class="line">    <span class="keyword">static</span> <span class="keyword">constexpr</span> T value = v;</span><br><span class="line">    <span class="keyword">using</span> value_type = T;</span><br><span class="line">    <span class="keyword">using</span> type       = integral_constant;</span><br><span class="line">    <span class="function"><span class="keyword">constexpr</span> <span class="keyword">operator</span>   <span class="title">value_type</span><span class="params">()</span> <span class="keyword">const</span> <span class="keyword">noexcept</span> </span>&#123; <span class="keyword">return</span> value; &#125;<span class="comment">// type conversion</span></span><br><span class="line">    <span class="function"><span class="keyword">constexpr</span> value_type <span class="title">operator</span><span class="params">()</span><span class="params">(<span class="comment">/*param-list*/</span>)</span> <span class="keyword">const</span> <span class="keyword">noexcept</span> </span>&#123; <span class="keyword">return</span> value; &#125;<span class="comment">// C++14 function-like</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">//bool helpers</span></span><br><span class="line"><span class="keyword">using</span> true_type  = integral_constant&lt;<span class="keyword">bool</span>, <span class="literal">true</span>&gt;;</span><br><span class="line"><span class="keyword">using</span> false_type = integral_constant&lt;<span class="keyword">bool</span>, <span class="literal">false</span>&gt;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span> &lt;<span class="keyword">bool</span> B&gt;</span><br><span class="line"><span class="keyword">using</span> bool_constant = integral_constant&lt;<span class="keyword">bool</span>, B&gt;;</span><br></pre></td></tr></table></figure></p>
<h3 id="enable-if-C-11"><a href="#enable-if-C-11" class="headerlink" title="enable_if C++11"></a>enable_if C++11</h3><p>SFINAE常用的检验类型的helper。如果<code>bool</code>为<code>true</code>，则有<code>typedef</code>，否则无法实例化，SFINAE跳过这个模板。<code>T</code>的默认类型为<code>void</code>。<br><figure class="highlight cpp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">template</span> &lt;<span class="keyword">bool</span> B, <span class="class"><span class="keyword">class</span> <span class="title">T</span> = <span class="title">void</span>&gt;</span></span><br><span class="line"><span class="class"><span class="title">struct</span> <span class="title">enable_if</span> &#123;</span>&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span> &lt;<span class="class"><span class="keyword">class</span> <span class="title">T</span>&gt;</span></span><br><span class="line"><span class="class"><span class="title">struct</span> <span class="title">enable_if</span>&lt;true, T&gt; &#123;</span> <span class="keyword">typedef</span> T type;&#125;;</span><br></pre></td></tr></table></figure></p>
<h3 id="conditional-C-11"><a href="#conditional-C-11" class="headerlink" title="conditional C++11"></a>conditional C++11</h3><p>编译期判断B，<code>true</code>则type为类型T，<code>false</code>为类型F。<br><figure class="highlight cpp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">template</span>&lt;<span class="keyword">bool</span> B, <span class="class"><span class="keyword">class</span> <span class="title">T</span>, <span class="title">class</span> <span class="title">F</span>&gt;</span></span><br><span class="line"><span class="class"><span class="title">struct</span> <span class="title">conditional</span> &#123;</span> <span class="keyword">typedef</span> T type;&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span>&lt;<span class="class"><span class="keyword">class</span> <span class="title">T</span>, <span class="title">class</span> <span class="title">F</span>&gt;</span></span><br><span class="line"><span class="class"><span class="title">struct</span> <span class="title">conditional</span>&lt;false, T, F&gt; &#123;</span> <span class="keyword">typedef</span> F type;&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span>&lt;<span class="keyword">bool</span> B, <span class="class"><span class="keyword">class</span> <span class="title">T</span>, <span class="title">class</span> <span class="title">F</span>&gt;</span></span><br><span class="line"><span class="class"><span class="title">using</span> <span class="title">conditional_t</span> = <span class="title">typename</span> <span class="title">conditional</span>&lt;B, T, F&gt;:</span>:type; <span class="comment">//C++14</span></span><br></pre></td></tr></table></figure></p>
<p>用<code>conditional</code>可以衍生出析取<code>disjunction</code>和合取<code>conjunction</code></p>
<h4 id="conjunction-C-17-amp-amp"><a href="#conjunction-C-17-amp-amp" class="headerlink" title="conjunction C++17 (&amp;&amp;)"></a>conjunction C++17 (&amp;&amp;)</h4><figure class="highlight cpp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">template</span> &lt;<span class="class"><span class="keyword">class</span> ...&gt; <span class="title">struct</span> <span class="title">conjunction</span> :</span> <span class="built_in">std</span>::true_type &#123;&#125;;</span><br><span class="line"><span class="keyword">template</span> &lt;<span class="class"><span class="keyword">class</span> <span class="title">B1</span>&gt; <span class="title">struct</span> <span class="title">conjunction</span>&lt;B1&gt;:</span> B1 &#123;&#125;;</span><br><span class="line"><span class="keyword">template</span> &lt;<span class="class"><span class="keyword">class</span> <span class="title">B1</span>, <span class="title">class</span>... <span class="title">Bn</span>&gt;</span></span><br><span class="line"><span class="class"><span class="title">struct</span> <span class="title">conjunction</span>&lt;B1, Bn...&gt;</span></span><br><span class="line"><span class="class">    :</span> <span class="built_in">std</span>::<span class="keyword">conditional_t</span>&lt;<span class="keyword">bool</span>(B1::value), conjunction&lt;Bn...&gt;, B1&gt; &#123;&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span>&lt;<span class="class"><span class="keyword">class</span>... <span class="title">B</span>&gt;</span></span><br><span class="line"><span class="class"><span class="title">inline</span> <span class="title">constexpr</span> <span class="title">bool</span> <span class="title">conjunction_v</span> = <span class="title">conjunction</span>&lt;B...&gt;:</span>:value;</span><br><span class="line"></span><br><span class="line"><span class="comment">//example</span></span><br><span class="line"><span class="keyword">template</span> &lt;<span class="class"><span class="keyword">class</span> <span class="title">T</span>, <span class="title">class</span> ...<span class="title">Ts</span>&gt;</span></span><br><span class="line"><span class="class"><span class="title">std</span>:</span>:<span class="keyword">enable_if_t</span>&lt;<span class="built_in">std</span>::conjunction_v&lt;<span class="built_in">std</span>::is_same&lt;T,Ts&gt;...&gt;&gt;</span><br><span class="line">func(T, Ts...) &#123;</span><br><span class="line">    <span class="built_in">std</span>::<span class="built_in">cout</span> &lt;&lt; <span class="string">"all type in pack are T\n"</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="disjunction-C-17"><a href="#disjunction-C-17" class="headerlink" title="disjunction C++17 (||)"></a>disjunction C++17 (||)</h4><p>找到第一个<code>value = true</code>的类型<br><figure class="highlight cpp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">template</span> &lt;<span class="class"><span class="keyword">class</span>...&gt; <span class="title">struct</span> <span class="title">dicjunction</span> :</span> <span class="built_in">std</span>::false_type &#123;&#125;;</span><br><span class="line"><span class="keyword">template</span> &lt;<span class="class"><span class="keyword">class</span> <span class="title">B1</span>&gt; <span class="title">struct</span> <span class="title">disjunction</span>&lt;B1&gt; :</span> B1&#123;&#125;;</span><br><span class="line"><span class="keyword">template</span> &lt;<span class="class"><span class="keyword">class</span> <span class="title">B1</span>, <span class="title">class</span>... <span class="title">Bn</span>&gt;</span></span><br><span class="line"><span class="class"><span class="title">struct</span> <span class="title">disjunction</span>&lt;B1, Bn...&gt;</span></span><br><span class="line"><span class="class">    :</span> <span class="built_in">std</span>::<span class="keyword">conditional_t</span>&lt;<span class="keyword">bool</span>(B1::value), B1, disjunction&lt;Bn...&gt;&gt; &#123;&#125;;</span><br></pre></td></tr></table></figure></p>
<h3 id="is-xxx-remove-xxx-add-xxx-C-11"><a href="#is-xxx-remove-xxx-add-xxx-C-11" class="headerlink" title="is_xxx, remove_xxx, add_xxx C++11"></a>is_xxx, remove_xxx, add_xxx C++11</h3><h4 id="is-same"><a href="#is-same" class="headerlink" title="is_same"></a>is_same</h4><figure class="highlight cpp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">template</span> &lt;<span class="class"><span class="keyword">class</span> <span class="title">T</span>, <span class="title">class</span> <span class="title">U</span>&gt; <span class="title">struct</span> <span class="title">is_same</span> :</span> <span class="built_in">std</span>::false_type &#123;&#125;;</span><br><span class="line"><span class="keyword">template</span> &lt;<span class="class"><span class="keyword">class</span> <span class="title">T</span>&gt; <span class="title">struct</span> <span class="title">is_same</span>&lt;T, T&gt; :</span> <span class="built_in">std</span>::true_type &#123;&#125;;</span><br></pre></td></tr></table></figure>
<h4 id="is-convertible"><a href="#is-convertible" class="headerlink" title="is_convertible"></a>is_convertible</h4><p>注意decltype中的逗号表达式，使用了SFINAE来检验类型。<br><figure class="highlight cpp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">template</span>&lt;<span class="class"><span class="keyword">class</span> <span class="title">T</span>&gt; // <span class="title">check</span> <span class="title">returnable</span></span></span><br><span class="line"><span class="class"><span class="title">auto</span> <span class="title">test_returnable</span>(<span class="title">int</span>) -&gt; <span class="title">decltype</span>(</span></span><br><span class="line"><span class="class">    <span class="title">void</span>(<span class="title">static_cast</span>&lt;T(*)()&gt;(<span class="title">nullptr</span>)), <span class="title">true_type</span>&#123;</span>&#125;</span><br><span class="line">);  </span><br><span class="line"><span class="keyword">template</span> &lt;<span class="class"><span class="keyword">class</span>&gt;</span></span><br><span class="line"><span class="class"><span class="title">auto</span> <span class="title">test_returnable</span>(...) -&gt; <span class="title">false_type</span>;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span>&lt;<span class="class"><span class="keyword">class</span> <span class="title">From</span>, <span class="title">class</span> <span class="title">To</span>&gt;// <span class="title">check</span> <span class="title">implicitly</span> <span class="title">convertible</span></span></span><br><span class="line"><span class="class"><span class="title">auto</span> <span class="title">test_implicitly_convertible</span>(<span class="title">int</span>) -&gt; <span class="title">decltype</span>(</span></span><br><span class="line"><span class="class">    <span class="title">void</span>(<span class="title">std</span>:</span>:declval&lt;<span class="keyword">void</span>(&amp;)(To)&gt;()(<span class="built_in">std</span>::declval&lt;From&gt;())), true_type&#123;&#125; </span><br><span class="line">);</span><br><span class="line"><span class="keyword">template</span>&lt;<span class="class"><span class="keyword">class</span>, <span class="title">class</span>&gt;</span></span><br><span class="line"><span class="class"><span class="title">auto</span> <span class="title">test_implicitly_convertible</span>(...) -&gt; <span class="title">false_type</span>;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span>&lt;<span class="class"><span class="keyword">class</span> <span class="title">From</span>, <span class="title">class</span> <span class="title">To</span>&gt;</span></span><br><span class="line"><span class="class"><span class="title">struct</span> <span class="title">is_convertible</span> :</span> <span class="built_in">std</span>::integral_constant&lt;<span class="keyword">bool</span>,</span><br><span class="line">    (<span class="keyword">decltype</span>(test_returnable&lt;To&gt;(<span class="number">0</span>))::value &amp;&amp;</span><br><span class="line">     <span class="keyword">decltype</span>(test_implicitly_convertible&lt;From, To&gt;(<span class="number">0</span>))::value) ||</span><br><span class="line">     (<span class="built_in">std</span>::is_void&lt;From&gt;::value &amp;&amp; <span class="built_in">std</span>::is_void&lt;To&gt;::value)</span><br><span class="line">&gt; &#123;&#125;; </span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span>&lt;<span class="class"><span class="keyword">class</span> <span class="title">From</span>, <span class="title">class</span> <span class="title">To</span>&gt;</span></span><br><span class="line"><span class="class"><span class="title">inline</span> <span class="title">constexpr</span> <span class="title">bool</span> <span class="title">is_convertible_v</span> = <span class="title">is_convertible</span>&lt;From, To&gt;:</span>:value; <span class="comment">//C++17</span></span><br></pre></td></tr></table></figure></p>
<h4 id="remove-cv"><a href="#remove-cv" class="headerlink" title="remove_cv"></a>remove_cv</h4><figure class="highlight cpp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">template</span> &lt;<span class="class"><span class="keyword">class</span> <span class="title">T</span>&gt; <span class="title">struct</span> <span class="title">remove_cv</span> &#123;</span> <span class="keyword">typedef</span> T type;&#125;;</span><br><span class="line"><span class="keyword">template</span> &lt;<span class="class"><span class="keyword">class</span> <span class="title">T</span>&gt; <span class="title">struct</span> <span class="title">remove_cv</span> &lt;const T&gt;&#123;</span> <span class="keyword">typedef</span> T type;&#125;;</span><br><span class="line"><span class="keyword">template</span> &lt;<span class="class"><span class="keyword">class</span> <span class="title">T</span>&gt; <span class="title">struct</span> <span class="title">remove_cv</span> &lt;volatile T&gt; &#123;</span> <span class="keyword">typedef</span> T type;&#125;;</span><br><span class="line"><span class="keyword">template</span> &lt;<span class="class"><span class="keyword">class</span> <span class="title">T</span>&gt; <span class="title">struct</span> <span class="title">remove_cv</span> &lt;const volatile T&gt;&#123;</span> <span class="keyword">typedef</span> T type;&#125;;</span><br></pre></td></tr></table></figure>
<h4 id="remove-reference"><a href="#remove-reference" class="headerlink" title="remove_reference"></a>remove_reference</h4><figure class="highlight cpp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">template</span> &lt;<span class="class"><span class="keyword">class</span> <span class="title">T</span>&gt; <span class="title">struct</span> <span class="title">remove_reference</span> &#123;</span> <span class="keyword">typedef</span> T type;&#125;;</span><br><span class="line"><span class="keyword">template</span> &lt;<span class="class"><span class="keyword">class</span> <span class="title">T</span>&gt; <span class="title">struct</span> <span class="title">remove_reference</span> &lt;T&amp;&gt; &#123;</span> <span class="keyword">typedef</span> T type;&#125;;</span><br><span class="line"><span class="keyword">template</span> &lt;<span class="class"><span class="keyword">class</span> <span class="title">T</span>&gt; <span class="title">struct</span> <span class="title">remove_reference</span> &lt;T&amp;&amp;&gt; &#123;</span> <span class="keyword">typedef</span> T type;&#125;;</span><br></pre></td></tr></table></figure>
<h4 id="remove-extent"><a href="#remove-extent" class="headerlink" title="remove_extent"></a>remove_extent</h4><figure class="highlight cpp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">template</span>&lt;<span class="class"><span class="keyword">class</span> <span class="title">T</span>&gt; <span class="title">struct</span> <span class="title">remove_extent</span> &#123;</span> <span class="keyword">typedef</span> T type; &#125;;</span><br><span class="line"><span class="keyword">template</span>&lt;<span class="class"><span class="keyword">class</span> <span class="title">T</span>&gt; <span class="title">struct</span> <span class="title">remove_extent</span>&lt;T[]&gt; &#123;</span> <span class="keyword">typedef</span> T type; &#125;;</span><br><span class="line"><span class="keyword">template</span>&lt;<span class="class"><span class="keyword">class</span> <span class="title">T</span>, <span class="title">std</span>:</span>:<span class="keyword">size_t</span> N&gt; <span class="class"><span class="keyword">struct</span> <span class="title">remove_extent</span>&lt;T[N]&gt; &#123;</span> <span class="keyword">typedef</span> T type; &#125;;</span><br></pre></td></tr></table></figure>
<h4 id="add-pointer"><a href="#add-pointer" class="headerlink" title="add_pointer"></a>add_pointer</h4><figure class="highlight cpp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">template</span> &lt;<span class="class"><span class="keyword">class</span> <span class="title">T</span>&gt;</span></span><br><span class="line"><span class="class"><span class="title">struct</span> <span class="title">type_identity</span> &#123;</span> <span class="keyword">using</span> type = T;&#125;;</span><br><span class="line"><span class="keyword">template</span> &lt;<span class="class"><span class="keyword">class</span> <span class="title">T</span>&gt;</span></span><br><span class="line"><span class="class"><span class="title">auto</span> <span class="title">try_add_pointer</span>(<span class="title">int</span>) -&gt; <span class="title">type_identity</span>&lt;typename std::remove_reference&lt;T&gt;::type*&gt;;</span></span><br><span class="line"><span class="keyword">template</span> &lt;<span class="class"><span class="keyword">class</span> <span class="title">T</span>&gt;</span></span><br><span class="line"><span class="class"><span class="title">auto</span> <span class="title">try_add_pointer</span>(...) -&gt; <span class="title">type_identity</span>&lt;T&gt;;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">template</span> &lt;<span class="class"><span class="keyword">class</span> <span class="title">T</span>&gt;</span></span><br><span class="line"><span class="class"><span class="title">struct</span> <span class="title">add_pointer</span> :</span> <span class="keyword">decltype</span>(try_add_pointer&lt;T&gt;(<span class="number">0</span>)) &#123;&#125;;</span><br></pre></td></tr></table></figure>
<h3 id="decay-C-11"><a href="#decay-C-11" class="headerlink" title="decay C++11"></a>decay C++11</h3><p>对T应用lvalue-to-rvalue, array-to-pointer, function-to-pointer的隐式转换，并去除cv修饰<br><figure class="highlight cpp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">template</span> &lt;<span class="class"><span class="keyword">class</span> <span class="title">T</span>&gt;</span></span><br><span class="line"><span class="class"><span class="title">struct</span> <span class="title">decay</span> &#123;</span></span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    <span class="keyword">typedef</span> <span class="keyword">typename</span> <span class="built_in">std</span>::remove_reference&lt;T&gt;::type U;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="keyword">typedef</span> <span class="keyword">typename</span> <span class="built_in">std</span>::conditional&lt;</span><br><span class="line">        <span class="built_in">std</span>::is_array&lt;U&gt;::value,</span><br><span class="line">        <span class="keyword">typename</span> <span class="built_in">std</span>::remove_extent&lt;U&gt;::type*,</span><br><span class="line">        <span class="keyword">typename</span> <span class="built_in">std</span>::conditional&lt;</span><br><span class="line">            <span class="built_in">std</span>::is_function&lt;U&gt;::value,</span><br><span class="line">            <span class="keyword">typename</span> <span class="built_in">std</span>::add_pointer&lt;U&gt;::type,</span><br><span class="line">            <span class="keyword">typename</span> <span class="built_in">std</span>::remove_cv&lt;U&gt;::type</span><br><span class="line">        &gt;::type</span><br><span class="line">    &gt;::type type;    </span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/template/" rel="tag"># template</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2021/05/25/Template-index-sequence-parameter-pack-fold-expression/" rel="next" title="[Template] index_sequence, parameter pack, fold expression">
                <i class="fa fa-chevron-left"></i> [Template] index_sequence, parameter pack, fold expression
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2021/08/14/Rendering-Pipelines/" rel="prev" title="Rendering Pipelines">
                Rendering Pipelines <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
      <div id="sidebar-dimmer"></div>
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/images/MOB.jpg" alt="HTelepathH">
            
              <p class="site-author-name" itemprop="name">HTelepathH</p>
              <p class="site-description motion-element" itemprop="description">Learning by doing.</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives">
              
                  <span class="site-state-item-count">25</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">12</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">15</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="99088577@qq.com" target="_blank" title="Mail">
                      Mail</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://gitee.com/htelepathh" target="_blank" title="Gitee">
                      Gitee</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/htlpt" target="_blank" title="GitHub">
                      GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://weibo.com/u/6207125886" target="_blank" title="微博">
                      微博</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://www.zhihu.com/people/huang-he-tu" target="_blank" title="知乎">
                      知乎</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#intergral-constant-C-11"><span class="nav-number">1.</span> <span class="nav-text">intergral_constant C++11</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#enable-if-C-11"><span class="nav-number">2.</span> <span class="nav-text">enable_if C++11</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#conditional-C-11"><span class="nav-number">3.</span> <span class="nav-text">conditional C++11</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#conjunction-C-17-amp-amp"><span class="nav-number">3.1.</span> <span class="nav-text">conjunction C++17 (&amp;&amp;)</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#disjunction-C-17"><span class="nav-number">3.2.</span> <span class="nav-text">disjunction C++17 (||)</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#is-xxx-remove-xxx-add-xxx-C-11"><span class="nav-number">4.</span> <span class="nav-text">is_xxx, remove_xxx, add_xxx C++11</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#is-same"><span class="nav-number">4.1.</span> <span class="nav-text">is_same</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#is-convertible"><span class="nav-number">4.2.</span> <span class="nav-text">is_convertible</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#remove-cv"><span class="nav-number">4.3.</span> <span class="nav-text">remove_cv</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#remove-reference"><span class="nav-number">4.4.</span> <span class="nav-text">remove_reference</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#remove-extent"><span class="nav-number">4.5.</span> <span class="nav-text">remove_extent</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#add-pointer"><span class="nav-number">4.6.</span> <span class="nav-text">add_pointer</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#decay-C-11"><span class="nav-number">5.</span> <span class="nav-text">decay C++11</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-angle-double-left"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">HTelepathH</span>

  
</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  
  


  

  

</body>
</html>
